﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CoilIntelStorage.Controller
{
    /// <summary>
    /// 总控中心
    /// </summary>
    public class GeneralControlCenter
    {
        /// <summary>
        /// 线程数量
        /// </summary>
        const int cycleNum = 10;
        static int cnt = 10;
        static AutoResetEvent myEvent = new AutoResetEvent(false);
        public GeneralControlCenter()
        {
            ThreadPool.SetMinThreads(1, 1);
            ThreadPool.SetMaxThreads(5, 5);
            for (int i = 1; i <= cycleNum; i++)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(testFun), i.ToString());
            }
            //Console.WriteLine("主线程执行！");
            //Console.WriteLine("主线程结束！");
            myEvent.WaitOne();
            //Console.WriteLine("线程池终止！");
            //Console.ReadKey();
        }
        public static void testFun(object obj)
        {
            cnt -= 1;
            //Console.WriteLine(string.Format("{0}:第{1}个线程", DateTime.Now.ToString(), obj.ToString()));
            //Thread.Sleep(5000);

            if (cnt == 0)
            {
                myEvent.Set();
            }
        }

    }
}
